package com.dtagri.fjwlw.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

/**  
 * @Title: DownLoadManager.java
 * @Package cn.com.ptnetwork.chinafair.util
 * @Description: (添加描述)
 * @author wangyong
 * @date 2012-6-19 上午10:50:37   
 * @version V1.0  
 * <p>Company:ptnetwork</p> 
 */
/**
 * @author wangyong
 * 
 */
public class DownLoadManager {
	public static final int UPDATA_CLIENT = 0;
	public static final int GET_UNDATAINFO_ERROR = 1;
	public static final int DOWN_ERROR = 2;
	public static final int START_MAIN = 3;
	public static final int PS_PROCESS = 4;
	public static final int PS_MAX = 5;
	public static final int PS_COMPLETE = 6;
	public static File getFileFromServer(String path, ProgressDialog pd,Handler handler,String strName)
			throws Exception {
		// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5000);
			// 获取到文件的大小
			//pd.setMax(conn.getContentLength() / 1024);
			Message msg1 = new Message();
			msg1.what = PS_MAX;
			Bundle data=new Bundle();
			data.putInt("max", conn.getContentLength() / 1024);
			msg1.setData(data);
			handler.sendMessage(msg1);
			
			
			InputStream is = conn.getInputStream();
			File file = new File(Environment.getExternalStorageDirectory(), strName);
			FileOutputStream fos = new FileOutputStream(file);
			BufferedInputStream bis = new BufferedInputStream(is);
			byte[] buffer = new byte[1024];
			int len;
			int total = 0;
			int temp = conn.getContentLength() / 30;
			int tempstep = 0;
			while ((len = bis.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
				total += len;
				// 获取当前下载量
				tempstep += len;
				if (tempstep >= temp) {
					tempstep = 0;
					Message msg = new Message();
					msg.what = PS_PROCESS;
					Bundle data2=new Bundle();
					data2.putInt("total", total/ 1024);
					msg.setData(data2);
					handler.sendMessage(msg);

				}
			}
			fos.close();
			bis.close();
			is.close();
			handler.sendEmptyMessage( PS_COMPLETE);
			return file;
		} else {
			return null;
		}
	}

//	public static String getBookFromServer(String path, ProgressDialog pd,Handler handler,String strName)
//			throws Exception {
//		// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
//		if (Environment.getExternalStorageState().equals(
//				Environment.MEDIA_MOUNTED)) {
//			URL url = new URL(path);
//			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//			conn.setConnectTimeout(5000);
//			// 获取到文件的大小
//			//pd.setMax(conn.getContentLength() / 1024);
//			Message msg1 = new Message();
//			msg1.what = PS_MAX;
//			Bundle data=new Bundle();
//			data.putInt("max", conn.getContentLength() / 1024);
//			msg1.setData(data);
//			handler.sendMessage(msg1);
//			
//			
//			InputStream is = conn.getInputStream();
//			File file = new File(Environment.getExternalStorageDirectory()+"/sannong/books/", strName);
//			FileOutputStream fos = new FileOutputStream(file);
//			BufferedInputStream bis = new BufferedInputStream(is);
//			byte[] buffer = new byte[1024];
//			int len;
//			int total = 0;
//			int temp = conn.getContentLength() / 30;
//			int tempstep = 0;
//			while ((len = bis.read(buffer)) != -1) {
//				fos.write(buffer, 0, len);
//				total += len;
//				// 获取当前下载量
//				tempstep += len;
//				if (tempstep >= temp) {
//					tempstep = 0;
//					Message msg = new Message();
//					msg.what = PS_PROCESS;
//					Bundle data2=new Bundle();
//					data2.putInt("total", total/ 1024);
//					msg.setData(data2);
//					handler.sendMessage(msg);
//
//				}
//			}
//			fos.close();
//			bis.close();
//			is.close();
//			handler.sendEmptyMessage( PS_COMPLETE);
//			return strName;
//		} else {
//			return null;
//		}
//	}
}
